﻿package
{
	import ASF;
	import org.flixel.FlxCore;
	import org.flixel.FlxSprite;
	import org.flixel.FlxG;
	import org.flixel.FlxEmitter;

	import ASPlayerShot;
	import StateGameLoop;
	
	import Delay;
	
	public class ASPlayer extends FlxSprite
	{
		[Embed(source = 'data/playership.png')] private var imgPlayerShip:Class;
		[Embed(source = 'data/srock1.png')] private var imgRock:Class;
		[Embed(source = 'data/explo2.mp3')] private var sndExplo2:Class;
		[Embed(source = 'data/hyperspace.mp3')] private var sndHyper:Class;
				
		private const MOVE_MAX_SPEED:int 	= 300;
		private const MOVE_DELTA:int 		= 25;
		
		private var _playerShots:Array;
		private var _parentState:StateGameLoop;
		
		private var _shootDelay:Delay = new Delay(ASF.PLAYER_SHOT_DELAY);
		
		private var _lastMouseX:int = 0;
		private var _lastMouseY:int = 0;
		
		/*----------------------------------------------*/
		public function ASPlayer(PlayerShots:Array, ParentState:StateGameLoop)
		{
			//super(imgPlayerShip, (FlxG.width / 2) - (width / 2), ASF.GROUND_LEVEL, false, false, 0, 0, 0xFF00FF00);
			super((FlxG.width / 2) - (width / 2), ASF.GROUND_LEVEL, imgPlayerShip);
			_playerShots = PlayerShots;
			_parentState = ParentState;
		}
		
		/*----------------------------------------------*/
		override public function update():void
		{
			_shootDelay.update();
			if (!flickering()) dead = false;
			
			if (FlxG.keys.RIGHT) {
				if (x >= (FlxG.width - width)) {
					velocity.x = 0;
					x = FlxG.width - width;
				} else if (velocity.x < MOVE_MAX_SPEED) {
					velocity.x += MOVE_DELTA;
				}
			}
			else if (FlxG.keys.LEFT) {
				if (x <= 0) {
					velocity.x = 0;
					x = 0;
				}
				else if (velocity.x > -MOVE_MAX_SPEED) {
					velocity.x -= MOVE_DELTA;
				}
			}
			else {
				if (velocity.x) {
					if (velocity.x > 0) {
						velocity.x-=MOVE_DELTA;
					} else {
						velocity.x+=MOVE_DELTA;
					}
				} else {
					x = int(x);
				}
			}
			
			if (FlxG.keys.justPressed("UP")) {
				hyperspace();
			}
			
			if (FlxG.keys.pressed("SPACE")) {
				if (_shootDelay.hasPassed()) {
					var shot:ASPlayerShot = FlxG.getNonexist(_playerShots) as ASPlayerShot;
					if (shot)
						shot.spawn(this.x + 5, ASF.GROUND_LEVEL-14);
					_shootDelay.reset();
				}
			}
					
			handleMouse();
			
			super.update();
		}

		/*----------------------------------------------*/
		public function handleMouse():void
		{
			if (FlxG.mouse.x != _lastMouseX) trace(FlxG.mouse.x);
			//_lastMouseX = FlxG.mouse.x;
			x = FlxG.mouse.x;
		}
		
		
		/*----------------------------------------------*/
		public function hyperspace():void
		{
			x = int(Math.random() * (FlxG.width - width));
			velocity.x = 0;
			FlxG.play(sndHyper);
		}
		
		/*----------------------------------------------*/
		override public function hurt(Damage:Number):void
		{
			FlxG.play(sndExplo2);
			exists = false;
			visible = false;
			dead = true;
			ASExplode(FlxG.getNonexist(_parentState._explosions)).spawn(x, y,0,true);
			_parentState.adjustScore( ASF.SCORE_PENALTY_DEATH );
			return;
		}
		/*----------------------------------------------*/
		public function respawn():void
		{
			exists = true;
			visible = true;
			x = (FlxG.width / 2) - (width / 2);
			flicker(3);
		}
	}
}